<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>手撕B树 | Eetal&#39;s Blog</title>

  <!-- keywords -->
  

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="欢迎查看Eetal的第十六篇博客–手撕B树 B树B树和B+树都是多路查找树。具备分支多层数少的优点常用于数据库索引存储BTree大量应用在数据库（mongodb）和文件系统当中，B+Tree经常用于数据库索引（空间局部性原理）B树代表每个节点可存储的数据节点不超过m-1，且每个节点的子节点不超过m个的树。根节点的子节点为0或至少为2B+树与B树区别就是B+树只在没有子节点的底层节点存储数据而其他包">
<meta property="og:type" content="article">
<meta property="og:title" content="手撕B树">
<meta property="og:url" content="http://eetal.gitee.io/blog/2019/04/03/手撕B树/index.html">
<meta property="og:site_name" content="Eetal&#39;s Blog">
<meta property="og:description" content="欢迎查看Eetal的第十六篇博客–手撕B树 B树B树和B+树都是多路查找树。具备分支多层数少的优点常用于数据库索引存储BTree大量应用在数据库（mongodb）和文件系统当中，B+Tree经常用于数据库索引（空间局部性原理）B树代表每个节点可存储的数据节点不超过m-1，且每个节点的子节点不超过m个的树。根节点的子节点为0或至少为2B+树与B树区别就是B+树只在没有子节点的底层节点存储数据而其他包">
<meta property="og:locale" content="default">
<meta property="og:image" content="http://eetal.gitee.io/blog/images/b-tree/b-tree.jpg">
<meta property="og:updated_time" content="2019-04-04T02:51:18.062Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="手撕B树">
<meta name="twitter:description" content="欢迎查看Eetal的第十六篇博客–手撕B树 B树B树和B+树都是多路查找树。具备分支多层数少的优点常用于数据库索引存储BTree大量应用在数据库（mongodb）和文件系统当中，B+Tree经常用于数据库索引（空间局部性原理）B树代表每个节点可存储的数据节点不超过m-1，且每个节点的子节点不超过m个的树。根节点的子节点为0或至少为2B+树与B树区别就是B+树只在没有子节点的底层节点存储数据而其他包">
<meta name="twitter:image" content="http://eetal.gitee.io/blog/images/b-tree/b-tree.jpg">
  
    <link rel="alternative" href="/atom.xml" title="Eetal&#39;s Blog" type="application/atom+xml">
  
  
    <link rel="icon" href="http://www.yangyitao.top/FiveChess/resource/ico/favicon.ico">
  
  <link rel="stylesheet" href="/blog/css/style.css">
  
  

  <script src="//cdn.bootcss.com/require.js/2.3.2/require.min.js"></script>
  <script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>

  
</head>
<body>
  <div id="container">
    <div id="particles-js"></div>
    <div class="left-col">
    <div class="overlay"></div>
<div class="intrude-less">
	<header id="header" class="inner">
		<a href="/" class="profilepic">
			
			<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			
		</a>

		<hgroup>
		  <h1 class="header-author"><a href="/">Eetal</a></h1>
		</hgroup>

		
		<p class="header-subtitle">java coder</p>
		

		
			<div class="switch-btn">
				<div class="icon">
					<div class="icon-ctn">
						<div class="icon-wrap icon-house" data-idx="0">
							<div class="birdhouse"></div>
							<div class="birdhouse_holes"></div>
						</div>
						<div class="icon-wrap icon-ribbon hide" data-idx="1">
							<div class="ribbon"></div>
						</div>
						
						<div class="icon-wrap icon-link hide" data-idx="2">
							<div class="loopback_l"></div>
							<div class="loopback_r"></div>
						</div>
						
						
					</div>
					
				</div>
				<div class="tips-box hide">
					<div class="tips-arrow"></div>
					<ul class="tips-inner">
						<li>菜单</li>
						<li>标签</li>
						
						<li>友情链接</li>
						
						
					</ul>
				</div>
			</div>
		

		<div class="switch-area">
			<div class="switch-wrap">
				<section class="switch-part switch-part1">
					<nav class="header-menu">
						<ul>
						
							<li><a href="/blog/">Home</a></li>
				        
							<li><a href="/blog/archives">Archives</a></li>
				        
						</ul>
					</nav>
					<nav class="header-nav">
						<div class="social">
							
						</div>
					</nav>
				</section>
				
				
				<section class="switch-part switch-part2">
					<div class="widget tagcloud" id="js-tagcloud">
						
					</div>
				</section>
				
				
				
				<section class="switch-part switch-part3">
					<div id="js-friends">
					
			          <a target="_blank" class="main-nav-link switch-friends-link" href="https://github.com/smackgg/hexo-theme-smackdown">smackdown</a>
			        
			        </div>
				</section>
				

				
			</div>
		</div>
	</header>				
</div>
    </div>
    <div class="mid-col">
      <nav id="mobile-nav">
  	<div class="overlay">
  		<div class="slider-trigger"></div>
  		<h1 class="header-author js-mobile-header hide">Eetal</h1>
  	</div>
	<div class="intrude-less">
		<header id="header" class="inner">
			<div class="profilepic">
				<img lazy-src="/blog/images/head.jpg" class="js-avatar">
			</div>
			<hgroup>
			  <h1 class="header-author">Eetal</h1>
			</hgroup>
			
			<p class="header-subtitle">java coder</p>
			
			<nav class="header-menu">
				<ul>
				
					<li><a href="/blog/">Home</a></li>
		        
					<li><a href="/blog/archives">Archives</a></li>
		        
		        <div class="clearfix"></div>
				</ul>
			</nav>
			<nav class="header-nav">
				<div class="social">
					
				</div>
			</nav>
		</header>				
	</div>
</nav>
      <div class="body-wrap"><article id="post-手撕B树" class="article article-type-post" itemscope itemprop="blogPost">
  
    <div class="article-meta">
      <a href="/blog/2019/04/03/手撕B树/" class="article-date">
  	<time datetime="2019-04-03T14:16:54.039Z" itemprop="datePublished">2019-04-03</time>
</a>
    </div>
  
  <div class="article-inner">
    
      <input type="hidden" class="isFancy" />
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      手撕B树
      
    </h1>
  

      </header>
      
      <div class="article-info article-info-post">
        
        

        
        <div class="clearfix"></div>
      </div>
      
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>欢迎查看Eetal的第十六篇博客–手撕B树</p>
<h2 id="B树"><a href="#B树" class="headerlink" title="B树"></a>B树</h2><p>B树和B+树都是多路查找树。具备分支多层数少的优点常用于数据库索引存储<br>BTree大量应用在数据库（mongodb）和文件系统当中，B+Tree经常用于数据库索引（空间局部性原理）<br>B树代表每个节点可存储的数据节点不超过m-1，且每个节点的子节点不超过m个的树。根节点的子节点为0或至少为2<br>B+树与B树区别就是B+树只在没有子节点的底层节点存储数据而其他包含子节点的父节点都是不存储数据值存储索引值<br>假设m为4我们就说这是一颗4叉的B树，其每个节点可以存储最多3个数据节点，最多含有4个子节点</p>
<h2 id="特点"><a href="#特点" class="headerlink" title="特点"></a>特点</h2><p>数据保存在节点<br>一个节点可以至有多m-1个数据节点<br>根结点如果有子女则至少有两个子女<br>除根结点以外的所有结点（不包括叶子结点）的度数正好是当前结点保存的数据节点总数加1，故内部子树个数 k 满足：┌m/2┐ &lt;= k &lt;= m<br>所有的叶子结点都位于同一层。（叶子节点指的是指向null）<br>每个非根节点所包含的数据节点个数 j 满足：┌m/2┐ - 1 &lt;= j &lt;= m - 1；// ┌m/2┐ ==》m/2向上取整</p>
<h2 id="结构图"><a href="#结构图" class="headerlink" title="结构图"></a>结构图</h2><p><img src="/blog/images/b-tree/b-tree.jpg" alt="b-tree"></p>
<h2 id="构造B树"><a href="#构造B树" class="headerlink" title="构造B树"></a>构造B树</h2><p>我们先了解下其几点重要构建要求<br>1.保持所有的叶子结点都位于同一层<br>2.一个节点可以至有多m-1个数据节点<br>3.每个非根节点所包含的数据节点个数 j 满足：┌m/2┐ - 1 &lt;= j &lt;= m - 1；// ┌m/2┐ ==》m/2向上取整<br>基于这些条件，B树的插入是从底部插入，先假设可以直接插入找到的节点，这时不破坏要求1<br>插入后因为新增数据节点如果破坏了要求2，进行裂变<br>裂变为了满足要求3从中间裂变<br>裂变出来的节点插入当前结点父节点（递归操作），因为是向上裂变插入，所以不破坏规则1</p>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><p>可以保存多个数据节点的节点类<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">class MultiNode&#123;</span><br><span class="line">	int nodeCounter = 0;//数据节点总数</span><br><span class="line">	Node head;//数据节点链表头</span><br><span class="line">	Node tail;//数据节点链表尾</span><br><span class="line">	MultiNode parent;//父节点</span><br><span class="line">	Node greaterParent;//大于当前节点的数据节点</span><br><span class="line">	Node lesserParent;//小于当前节点的数据节点</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>数据节点类<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">class Node&#123;</span><br><span class="line">	int num;//索引大小</span><br><span class="line">	MultiNode greater;//大于</span><br><span class="line">	MultiNode lesser;//小于</span><br><span class="line">	Node prev;</span><br><span class="line">	Node next;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>裂变操作<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line">private Node fissAndReturnBreakNode(MultiNode multiNode,int breakPoint) &#123;</span><br><span class="line">	<span class="keyword">if</span>(breakPoint&lt;1 || breakPoint&gt;=multiNums) &#123;</span><br><span class="line">		<span class="built_in">return</span> null;</span><br><span class="line">	&#125;</span><br><span class="line">	//裂变</span><br><span class="line">	<span class="keyword">if</span>(multiNode.greaterParent!=null) &#123;</span><br><span class="line">		//父节点断开</span><br><span class="line">		multiNode.greaterParent.lesser = null;</span><br><span class="line">		multiNode.greaterParent = null;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(multiNode.lesserParent!=null) &#123;</span><br><span class="line">		//父节点断开</span><br><span class="line">		multiNode.lesserParent.greater = null;</span><br><span class="line">		multiNode.lesserParent = null;</span><br><span class="line">	&#125;</span><br><span class="line">	Node beakNode = multiNode.head;</span><br><span class="line">	<span class="keyword">for</span>(int i=0;i&lt;breakPoint;i++)</span><br><span class="line">		beakNode = beakNode.next;</span><br><span class="line">	MultiNode newMultiNode = new MultiNode();</span><br><span class="line">	newMultiNode.head = beakNode.next;</span><br><span class="line">	newMultiNode.tail = multiNode.tail;</span><br><span class="line">	multiNode.tail = beakNode.prev;</span><br><span class="line">	newMultiNode.nodeCounter = multiNode.nodeCounter - breakPoint -1;</span><br><span class="line">	multiNode.nodeCounter = breakPoint;</span><br><span class="line">	beakNode.next = beakNode.prev = newMultiNode.head.prev = multiNode.tail.next = null;</span><br><span class="line">	<span class="keyword">if</span>(beakNode.lesser!=null) &#123;</span><br><span class="line">		beakNode.lesser.lesserParent = multiNode.tail;</span><br><span class="line">		multiNode.tail.greater = beakNode.lesser;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">if</span>(beakNode.greater!=null) &#123;</span><br><span class="line">		beakNode.greater.greaterParent = newMultiNode.head;</span><br><span class="line">		newMultiNode.head.lesser = beakNode.greater;</span><br><span class="line">	&#125;</span><br><span class="line">	beakNode.lesser = multiNode;</span><br><span class="line">	multiNode.greaterParent = beakNode;</span><br><span class="line">	beakNode.greater = newMultiNode;</span><br><span class="line">	newMultiNode.lesserParent = beakNode;</span><br><span class="line">	newMultiNode.parent = multiNode.parent;</span><br><span class="line">	<span class="built_in">return</span> beakNode;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>插入操作<br><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br></pre></td><td class="code"><pre><span class="line">public void addNum(int num) &#123;</span><br><span class="line">	Node node = new Node();</span><br><span class="line">	node.num = num;</span><br><span class="line">	addNode(node,root);</span><br><span class="line">&#125;</span><br><span class="line">protected MultiNode addNode(Node node,MultiNode multiNode) &#123;</span><br><span class="line">	<span class="keyword">if</span>(node == null) &#123;</span><br><span class="line">		<span class="built_in">return</span> multiNode;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span> <span class="keyword">if</span>(multiNode == null) &#123;</span><br><span class="line">		root = multiNode = new MultiNode();</span><br><span class="line">		multiNode.tail = multiNode.head = node;</span><br><span class="line">		multiNode.nodeCounter++;</span><br><span class="line">		<span class="built_in">return</span> multiNode;</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="keyword">else</span>&#123;</span><br><span class="line">		Node head = multiNode.head;</span><br><span class="line">		<span class="keyword">do</span>&#123;</span><br><span class="line">			<span class="keyword">if</span>(head.num &gt; node.num) &#123;</span><br><span class="line">				<span class="keyword">if</span>(head.lesser!=null) &#123;</span><br><span class="line">					addNode(node,head.lesser);</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="keyword">else</span> &#123;</span><br><span class="line">					<span class="keyword">if</span>(head.prev!=null) &#123;</span><br><span class="line">						<span class="keyword">if</span>(head.lesser != null) &#123;</span><br><span class="line">							node.greater = head.lesser;</span><br><span class="line">							head.lesser.lesserParent = node;</span><br><span class="line">						&#125;</span><br><span class="line">						<span class="keyword">if</span>(head.prev.greater!=null) &#123;</span><br><span class="line">							node.lesser = head.prev.greater;</span><br><span class="line">							head.prev.greater.greaterParent = node;</span><br><span class="line">						&#125;</span><br><span class="line">						head.prev.next = node;</span><br><span class="line">						node.prev = head.prev;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> &#123;</span><br><span class="line">						<span class="keyword">if</span>(head.lesser!=null) &#123;</span><br><span class="line">							node.greater = head.lesser;</span><br><span class="line">							head.lesser.lesserParent = node;</span><br><span class="line">						&#125;</span><br><span class="line">						multiNode.head = node;</span><br><span class="line">					&#125;</span><br><span class="line">					node.next = head;</span><br><span class="line">					head.prev = node;</span><br><span class="line">					<span class="keyword">if</span>(++multiNode.nodeCounter == multiNums) &#123;</span><br><span class="line">						Node breakNode = fissAndReturnBreakNode(multiNode,multiNums/2);</span><br><span class="line">						addNode(breakNode,multiNode.parent);</span><br><span class="line">					&#125;</span><br><span class="line">				&#125;</span><br><span class="line">				<span class="built_in">return</span> multiNode;</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;<span class="keyword">while</span>((head = head.next)!=null);</span><br><span class="line">		</span><br><span class="line">		<span class="keyword">if</span>(multiNode.tail.greater!=null) &#123;</span><br><span class="line">			addNode(node,multiNode.tail.greater);</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> &#123;</span><br><span class="line">			multiNode.tail.next = node;</span><br><span class="line">			node.prev = multiNode.tail;</span><br><span class="line">			<span class="keyword">if</span>(multiNode.tail.greater!=null) &#123;</span><br><span class="line">				node.lesser = multiNode.tail.greater;</span><br><span class="line">				multiNode.tail.greater.greaterParent = node;</span><br><span class="line">			&#125;</span><br><span class="line">			multiNode.tail = node;</span><br><span class="line">			<span class="keyword">if</span>(++multiNode.nodeCounter == multiNums) &#123;</span><br><span class="line">				Node breakNode = fissAndReturnBreakNode(multiNode,multiNums/2);</span><br><span class="line">				addNode(breakNode,multiNode.parent);</span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="built_in">return</span> multiNode;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="完整代码"><a href="#完整代码" class="headerlink" title="完整代码"></a>完整代码</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br></pre></td><td class="code"><pre><span class="line">package zcy;</span><br><span class="line"></span><br><span class="line">import java.util.concurrent.LinkedBlockingQueue;</span><br><span class="line"></span><br><span class="line">public class BTree &#123;</span><br><span class="line"></span><br><span class="line">	public BTree(int multiNums) &#123;</span><br><span class="line">		this.multiNums = multiNums;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	volatile MultiNode root;//根节点</span><br><span class="line">	</span><br><span class="line">	int multiNums;//叉的数目</span><br><span class="line">	</span><br><span class="line">	public static void printTree(MultiNode node) &#123;</span><br><span class="line">		<span class="keyword">if</span>(node == null)</span><br><span class="line">			<span class="built_in">return</span>;</span><br><span class="line">		System.out.println(node+<span class="string">": "</span>);</span><br><span class="line">		LinkedBlockingQueue&lt;MultiNode&gt; queue = new LinkedBlockingQueue&lt;MultiNode&gt;();</span><br><span class="line">		Node head = node.head; </span><br><span class="line">		<span class="keyword">while</span>(head!=null) &#123;</span><br><span class="line">			<span class="keyword">if</span>(head.lesser!=null)</span><br><span class="line">				queue.add(head.lesser);</span><br><span class="line">			<span class="keyword">if</span>(head.greater!=null)</span><br><span class="line">				queue.add(head.greater);</span><br><span class="line">			System.out.println(head.num+<span class="string">" -&gt; lesser:"</span>+head.lesser+<span class="string">", -&gt;greate:"</span>+head.greater);</span><br><span class="line">			head = head.next;</span><br><span class="line">		&#125;</span><br><span class="line">		System.out.println();</span><br><span class="line">		queue.forEach(multiNode-&gt;printTree(multiNode));</span><br><span class="line">	&#125;</span><br><span class="line">	class MultiNode&#123;</span><br><span class="line">		int nodeCounter = 0;//数据节点总数</span><br><span class="line">		Node head;//数据节点链表头</span><br><span class="line">		Node tail;//数据节点链表尾</span><br><span class="line">		MultiNode parent;//父节点</span><br><span class="line">		Node greaterParent;//大父节点</span><br><span class="line">		Node lesserParent;//大父节点</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	class Node&#123;</span><br><span class="line">		int num;//索引大小</span><br><span class="line">		MultiNode greater;//大于</span><br><span class="line">		MultiNode lesser;//小于</span><br><span class="line">		Node prev;</span><br><span class="line">		Node next;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public void addNum(int num) &#123;</span><br><span class="line">		Node node = new Node();</span><br><span class="line">		node.num = num;</span><br><span class="line">		addNode(node,root);</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	private Node fissAndReturnBreakNode(MultiNode multiNode,int breakPoint) &#123;</span><br><span class="line">		<span class="keyword">if</span>(breakPoint&lt;1 || breakPoint&gt;=multiNums) &#123;</span><br><span class="line">			<span class="built_in">return</span> null;</span><br><span class="line">		&#125;</span><br><span class="line">		//裂变</span><br><span class="line">		<span class="keyword">if</span>(multiNode.greaterParent!=null) &#123;</span><br><span class="line">			//父节点断开</span><br><span class="line">			multiNode.greaterParent.lesser = null;</span><br><span class="line">			multiNode.greaterParent = null;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(multiNode.lesserParent!=null) &#123;</span><br><span class="line">			//父节点断开</span><br><span class="line">			multiNode.lesserParent.greater = null;</span><br><span class="line">			multiNode.lesserParent = null;</span><br><span class="line">		&#125;</span><br><span class="line">		Node beakNode = multiNode.head;</span><br><span class="line">		<span class="keyword">for</span>(int i=0;i&lt;breakPoint;i++)</span><br><span class="line">			beakNode = beakNode.next;</span><br><span class="line">		MultiNode newMultiNode = new MultiNode();</span><br><span class="line">		newMultiNode.head = beakNode.next;</span><br><span class="line">		newMultiNode.tail = multiNode.tail;</span><br><span class="line">		multiNode.tail = beakNode.prev;</span><br><span class="line">		newMultiNode.nodeCounter = multiNode.nodeCounter - breakPoint -1;</span><br><span class="line">		multiNode.nodeCounter = breakPoint;</span><br><span class="line">		beakNode.next = beakNode.prev = newMultiNode.head.prev = multiNode.tail.next = null;</span><br><span class="line">		<span class="keyword">if</span>(beakNode.lesser!=null) &#123;</span><br><span class="line">			beakNode.lesser.lesserParent = multiNode.tail;</span><br><span class="line">			multiNode.tail.greater = beakNode.lesser;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">if</span>(beakNode.greater!=null) &#123;</span><br><span class="line">			beakNode.greater.greaterParent = newMultiNode.head;</span><br><span class="line">			newMultiNode.head.lesser = beakNode.greater;</span><br><span class="line">		&#125;</span><br><span class="line">		beakNode.lesser = multiNode;</span><br><span class="line">		multiNode.greaterParent = beakNode;</span><br><span class="line">		beakNode.greater = newMultiNode;</span><br><span class="line">		newMultiNode.lesserParent = beakNode;</span><br><span class="line">		newMultiNode.parent = multiNode.parent;</span><br><span class="line">		<span class="built_in">return</span> beakNode;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	protected MultiNode addNode(Node node,MultiNode multiNode) &#123;</span><br><span class="line">		<span class="keyword">if</span>(node == null) &#123;</span><br><span class="line">			<span class="built_in">return</span> multiNode;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span> <span class="keyword">if</span>(multiNode == null) &#123;</span><br><span class="line">			root = multiNode = new MultiNode();</span><br><span class="line">			multiNode.tail = multiNode.head = node;</span><br><span class="line">			multiNode.nodeCounter++;</span><br><span class="line">			<span class="built_in">return</span> multiNode;</span><br><span class="line">		&#125;</span><br><span class="line">		<span class="keyword">else</span>&#123;</span><br><span class="line">			Node head = multiNode.head;</span><br><span class="line">			<span class="keyword">do</span>&#123;</span><br><span class="line">				<span class="keyword">if</span>(head.num &gt; node.num) &#123;</span><br><span class="line">					<span class="keyword">if</span>(head.lesser!=null) &#123;</span><br><span class="line">						addNode(node,head.lesser);</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="keyword">else</span> &#123;</span><br><span class="line">						<span class="keyword">if</span>(head.prev!=null) &#123;</span><br><span class="line">							<span class="keyword">if</span>(head.lesser != null) &#123;</span><br><span class="line">								node.greater = head.lesser;</span><br><span class="line">								head.lesser.lesserParent = node;</span><br><span class="line">							&#125;</span><br><span class="line">							<span class="keyword">if</span>(head.prev.greater!=null) &#123;</span><br><span class="line">								node.lesser = head.prev.greater;</span><br><span class="line">								head.prev.greater.greaterParent = node;</span><br><span class="line">							&#125;</span><br><span class="line">							head.prev.next = node;</span><br><span class="line">							node.prev = head.prev;</span><br><span class="line">						&#125;</span><br><span class="line">						<span class="keyword">else</span> &#123;</span><br><span class="line">							<span class="keyword">if</span>(head.lesser!=null) &#123;</span><br><span class="line">								node.greater = head.lesser;</span><br><span class="line">								head.lesser.lesserParent = node;</span><br><span class="line">							&#125;</span><br><span class="line">							multiNode.head = node;</span><br><span class="line">						&#125;</span><br><span class="line">						node.next = head;</span><br><span class="line">						head.prev = node;</span><br><span class="line">						<span class="keyword">if</span>(++multiNode.nodeCounter == multiNums) &#123;</span><br><span class="line">							Node breakNode = fissAndReturnBreakNode(multiNode,multiNums/2);</span><br><span class="line">							addNode(breakNode,multiNode.parent);</span><br><span class="line">						&#125;</span><br><span class="line">					&#125;</span><br><span class="line">					<span class="built_in">return</span> multiNode;</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;<span class="keyword">while</span>((head = head.next)!=null);</span><br><span class="line">			</span><br><span class="line">			<span class="keyword">if</span>(multiNode.tail.greater!=null) &#123;</span><br><span class="line">				addNode(node,multiNode.tail.greater);</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="keyword">else</span> &#123;</span><br><span class="line">				multiNode.tail.next = node;</span><br><span class="line">				node.prev = multiNode.tail;</span><br><span class="line">				<span class="keyword">if</span>(multiNode.tail.greater!=null) &#123;</span><br><span class="line">					node.lesser = multiNode.tail.greater;</span><br><span class="line">					multiNode.tail.greater.greaterParent = node;</span><br><span class="line">				&#125;</span><br><span class="line">				multiNode.tail = node;</span><br><span class="line">				<span class="keyword">if</span>(++multiNode.nodeCounter == multiNums) &#123;</span><br><span class="line">					Node breakNode = fissAndReturnBreakNode(multiNode,multiNums/2);</span><br><span class="line">					addNode(breakNode,multiNode.parent);</span><br><span class="line">				&#125;</span><br><span class="line">			&#125;</span><br><span class="line">			<span class="built_in">return</span> multiNode;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	public static void main(String[] args) &#123;</span><br><span class="line">		BTree tree = new BTree(4);//4叉B树每个multi最多含有3个节点</span><br><span class="line">		<span class="keyword">for</span>(int i=0;i&lt;7;i++)</span><br><span class="line">			tree.addNum(i);</span><br><span class="line">		BTree.printTree(tree.root);</span><br><span class="line">	&#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="请移步"><a href="#请移步" class="headerlink" title="请移步"></a>请移步</h3><p>个人主页: <a href="http://yangyitao.top" target="_blank" rel="noopener">yangyitao.top</a></p>

      
    </div>
    
  </div>
  
    
<nav id="article-nav">
  
    <a href="/blog/2019/04/07/动态代理与静态代理/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">&lt;</strong>
      <div class="article-nav-title">
        
          动态代理与静态代理
        
      </div>
    </a>
  
  
    <a href="/blog/2019/03/31/手撕AVL树/" id="article-nav-older" class="article-nav-link-wrap">
      <div class="article-nav-title">手撕AVL树</div>
      <strong class="article-nav-caption">&gt;</strong>
    </a>
  
</nav>

  
</article>


<div class="ds-share share" data-thread-key="手撕B树" data-title="手撕B树" data-url="http://eetal.gitee.io/blog/blog/2019/04/03/手撕B树/"  data-images="/blog/images/head.jpg" data-content="手撕B树">
    <div class="ds-share-inline">
      <ul  class="ds-share-icons-16">
      	<li data-toggle="ds-share-icons-more"><a class="ds-more" href="javascript:void(0);">分享到：</a></li>
        <li><a class="ds-weibo" href="javascript:void(0);" data-service="weibo">微博</a></li>
        <li><a class="ds-qzone" href="javascript:void(0);" data-service="qzone">QQ空间</a></li>
        <li><a class="ds-qqt" href="javascript:void(0);" data-service="qqt">腾讯微博</a></li>
        <li><a class="ds-wechat" href="javascript:void(0);" data-service="wechat">微信</a></li>
      </ul>
      <div class="ds-share-icons-more">
      </div>
    </div>
 </div>
 





</div>
      <footer id="footer">
  <div class="outer">
    <div id="footer-info">
      <div class="footer-left">
        &copy; 2019 Eetal
      </div>
        <div class="footer-right">
          <a href="http://hexo.io/" target="_blank">Hexo</a>  Theme <a href="https://github.com/smackgg/hexo-theme-smackdown" target="_blank">Smackdown</a>
        </div>
    </div>
  </div>
</footer>
    </div>
    
  <link rel="stylesheet" href="/blog/fancybox/jquery.fancybox.css">


<script>
	var yiliaConfig = {
		fancybox: true,
		mathjax: true,
		animate: true,
		isHome: false,
		isPost: true,
		isArchive: false,
		isTag: false,
		isCategory: false,
		open_in_new: false
	}
</script>
<script src="/blog/js/main.js"></script>



<script type="text/x-mathjax-config">
MathJax.Hub.Config({
    tex2jax: {
        inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
        processEscapes: true,
        skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
    }
});

MathJax.Hub.Queue(function() {
    var all = MathJax.Hub.getAllJax(), i;
    for(i=0; i < all.length; i += 1) {
        all[i].SourceElement().parentNode.className += ' has-jax';                 
    }       
});
</script>

<script src="//cdn.bootcss.com/mathjax/2.7.0/MathJax.js"></script>


  </div>
</body>
</html>